<?php
class Player extends User
{
    public $friends = null;

    public function create()
    {
        $data = array();
        $data['user_id'] = $this->user_id;
        foreach(_settings::$default_profile as $field => $value)
        {
            $data[$field] = $value;
        }

        $sql = null;
        foreach($data as $f => $v)
        {
            $sql[] = '`'.$f.'`=\''.mysql_escape_string($v).'\'';
        }

        $query = 'INSERT INTO players SET '.implode(',',$sql);
        $r = _database::query($query);
        if($r)
        {
            $this->data = $data;
            Statistic::add('register_30', 1);
            return $data;
        }
        return false;
    }

    public function addMoney($amount)
    {
        $this->setProperty('money', $this->data['money'] + max(0,$amount));
    }

    public function getFriendProfiles($need = array('user_id','exp','profession','location','lastLevel'))
    {
        $out = array();
        if(is_array($this->friends) && count($this->friends))
        {
            $query = 'SELECT * FROM `players` WHERE `user_id` IN ('.implode(',',$this->friends).')';
            $data = _database::toarray($query);
            if(is_array($data) && count($data))
            {
                foreach($data as $datarow)
                {
                    $friend = new Player($datarow['user_id']);
                    $out[$datarow['user_id']] = $friend->getProfileClean($need,$datarow);
                    $out[$datarow['user_id']]['visited'] = 0;
                }
            }

            $query = 'SELECT `friend_id` FROM players_friendbonus WHERE `user_id` = '.$this->user_id;
            $visited = _database::toarray($query);
            if(is_array($visited) && count($visited))
            {
                foreach($visited as $row)
                {
                    if(isset($out[$row['friend_id']]))
                    {
                        $out[$row['friend_id']]['visited'] = 1;
                    }
                }
            }
        }
        return $out;
    }

    public function saveFriendsString($string)
    {
        if(!$string)
        {
            $this->friends = array();
            return true;
        }
        $friends = explode(',',$string);
        foreach($friends as $uid)
        {
            if($uid)
            {
                if(is_numeric($uid))
                $this->friends[$uid] = $uid;
            }
        }
        if(is_array($this->friends) && count($this->friends))
        {
            $query = 'INSERT INTO `players_friends` SET `user_id`='.$this->user_id.', `friends`=\''.implode(',',$friends).'\'
            ON DUPLICATE KEY UPDATE `friends`=\''.implode(',',$friends).'\'';
            _database::query($query);
        }
        return true;
    }

    // return profile of user
    public function getProfile($data = false)
    {
        if($data)
        {
            $this->data = $data; // rewrite only for friends (mass fetching)
        }
        if(is_array($this->data) && count($this->data))
        {
            // mysql fetched already
            return $this->data;
        }
        else return $this->is_registered();
    }

    // return profile for client
    public function getProfileClean($need = array(
'user_id','profession','exp',
'lastLevel','money','money_gold',
'skill_hp','skill_def','skill_str',
'skill_weight','skill_mage','skill_blacksmith',
'skill_cook','skill_jewel','lastGetBonus',
'lastBonusDay','location','location_next','location_next_time','tutorial'),$data = false)
    {
        if(!$data)
        $data = $this->getProfile();
        if(is_array($data) && count($data))
        {
            // mysql fetched already
            $out = array();
            foreach($data as $f => $v)
            {
                if(in_array($f, $need))
                {
                    $out[$f] = $v;
                }
            }
            return $out;
        }
        return false;
    }
}
?>
